//
//  MeEditAddressCell.m
//  Ginix
//
//  Created by fzq on 2025/3/3.
//

#import "MeEditAddressCell.h"

@interface MeEditAddressCell()
@property (nonatomic,strong) UIView * line;
@property (nonatomic,strong) UITextField * infoFiled;
@property (nonatomic,strong) UIImageView * arrow;
@property (nonatomic,strong) UILabel * nameLable;
@property (nonatomic,strong) UISwitch * noSwitch;
@property (nonatomic,assign) NSInteger index;
@property (nonatomic,strong) AddressListModel * infoModel;

@end

@implementation MeEditAddressCell

- (void)awakeFromNib {
    [super awakeFromNib];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setSubViewLayout];
    }
    return self;
}
-(void)setSubViewLayout{
    
    [self addSubview:self.line];
    [_line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_offset(CGSizeMake(345 * FMScaleX, 2 ));
        make.centerX.bottom.equalTo(@0);
    }];
    [self.contentView addSubview:self.infoFiled];
    [self.contentView addSubview:self.arrow];
    [self.contentView addSubview:self.noSwitch];
}

-(void)setInfoWithIndex:(NSInteger )index model: (AddressListModel *)model{
    
    self.index = index;
    self.infoModel = model;
    
    if (index == 2 || index == 4) {
        self.infoFiled.userInteractionEnabled = NO;
    }else{
        self.infoFiled.userInteractionEnabled =  YES;
    }
    self.arrow.hidden = index != 2;
    self.noSwitch.hidden = index != 4;
    switch (index) {
        case 0:
            self.nameLable.text = @"收货人";
            self.infoFiled.attributedPlaceholder = ({
                NSAttributedString * string = [[NSAttributedString alloc]initWithString:@"请输入收货人姓名" attributes:@{NSForegroundColorAttributeName:RDRGB(0x8588AA),NSFontAttributeName:Regular(14)}];
                string;
            });
            self.infoFiled.text = model.name?:@"";
            break;
        case 1:
            self.nameLable.text = @"联系电话";
            self.infoFiled.attributedPlaceholder = ({
                NSAttributedString * string = [[NSAttributedString alloc]initWithString:@"请填写收货人手机号码" attributes:@{NSForegroundColorAttributeName:RDRGB(0x8588AA),NSFontAttributeName:Regular(14)}];
                string;
            });
            self.infoFiled.text = model.phone ?:@"";
            self.infoFiled.keyboardType = UIKeyboardTypeNumberPad;

            break;
        case 2:
            self.nameLable.text = @"选择地区";
            self.infoFiled.attributedPlaceholder = ({
                NSAttributedString * string = [[NSAttributedString alloc]initWithString:@"请选择收件地址（省、市、区）" attributes:@{NSForegroundColorAttributeName:RDRGB(0x8588AA),NSFontAttributeName:Regular(14)}];
                string;
            });
            self.infoFiled.text = [NSString stringWithFormat:@"%@%@%@",model.province ?: @"",model.city?:@"",model.district?:@""];

            break;
        case 3:
            self.nameLable.text = @"详细地址";
            self.infoFiled.attributedPlaceholder = ({
                NSAttributedString * string = [[NSAttributedString alloc]initWithString:@"例如街道、门牌号、小区、单元等" attributes:@{NSForegroundColorAttributeName:RDRGB(0x8588AA),NSFontAttributeName:Regular(14)}];
                string;
            });
            self.infoFiled.keyboardType = UIKeyboardTypeDefault;
            self.infoFiled.text = model.detailAddress?:@"";

            break;
            
        case 4:
            self.nameLable.text = @"设为默认地址";
            self.infoFiled.attributedPlaceholder = ({
                NSAttributedString * string = [[NSAttributedString alloc]initWithString:@"" attributes:@{NSForegroundColorAttributeName:RDRGB(0x8588AA),NSFontAttributeName:Regular(14)}];
                string;
            });
            self.infoFiled.text = @"";

            break;
            
        default:
            break;
    }
    if (model.type == 1) {
        [self.noSwitch setOn:YES];
    }else{
        [self.noSwitch setOn:NO];
    }
}

-(void)noSwitchAction:(UISwitch *)sender{
    
    if (sender.isOn) {
        self.infoModel.type = 1;
    }else{
        self.infoModel.type = 0;
    }
}

-(void)textFieldDidChange:(UITextField *)sender{
    
    if (self.index == 0) {
        self.infoModel.name = sender.text;
    }
    if (self.index == 1) {
        self.infoModel.phone = sender.text;
    }
    if (self.index == 3) {
        self.infoModel.detailAddress = sender.text;

    }
}

-(UIView *)line{
    
    if (!_line) {
        _line = [[UIView alloc]init];
        _line.backgroundColor = RDRGB(0xF8F7F4);
    }
    return _line;
}

-(UITextField *)infoFiled{
    
    if (!_infoFiled) {
        _infoFiled = [[UITextField alloc]initWithFrame:CGRectMake( 0, 5 * FMScaleX, WIDTH, 50 * FMScaleX)];
        _infoFiled.font = Regular(14);
        _infoFiled.textColor = RDRGB(0x333333);
        _infoFiled.leftViewMode = UITextFieldViewModeAlways;
        _infoFiled.leftView = ({
            UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 120*FMScaleX, 50*FMScaleX)];
            [view addSubview:self.nameLable];
            view;
        });
        [_infoFiled addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    }
    return _infoFiled;
}
-(UILabel *)nameLable{
    
    if (!_nameLable) {
        _nameLable = [[UILabel alloc]initWithFrame:CGRectMake(20 * FMScaleX, 0 * FMScaleX, 100 * FMScaleX, 50 * FMScaleX)];
        _nameLable.textColor = RDRGB(0x666666);
        _nameLable.font = Regular(14);
    }
    return _nameLable;
}

-(UIImageView *)arrow{
    
    if (!_arrow) {
        _arrow = [[UIImageView alloc]initWithFrame:CGRectMake(WIDTH - 40 * FMScaleX, 16 * FMScaleX, 24 * FMScaleX, 24 * FMScaleX)];
        _arrow.image = [UIImage imageNamed:@"Me_head_arrow"];
    }
    return _arrow;
}
-(UISwitch *)noSwitch{
    
    if (!_noSwitch) {
        _noSwitch = [[UISwitch alloc]initWithFrame:CGRectMake(WIDTH - 66 * FMScaleX, 12 * FMScaleX, 60 * FMScaleX, 32 * FMScaleX)];
        [_noSwitch setOnTintColor:RDRGB(0xE8DFD4)];
        [_noSwitch setTintColor:RDRGB(0xCCCCCC)];
        [_noSwitch addTarget:self action:@selector(noSwitchAction:) forControlEvents:UIControlEventValueChanged];
    }
    return _noSwitch;
}
@end
